<?php

namespace app\backstage\controller;


use app\model\Task as model;
use app\model\FreshAdminUser;
use app\model\FreshAdminAuthGroupAccess;
use app\model\TaskRecord;
use app\model\UserAward;
use app\util\Tools;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
use app\util\ReturnCode;
use think\Db;

class Award extends Base {



    public function logList(){
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $postData = $this->request->get();

        $where = function ($query)use($postData){
            if(isset($postData['turnover']) && $postData['turnover']){
                $query->where('i.turnover', $postData['turnover']);
            }
            if(isset($postData['beginTime']) && $postData['beginTime']){
                $query->where('i.addtime', '>=', strtotime($postData['beginTime']));
            }
            if(isset($postData['endTime']) && $postData['endTime']){
                $endTime = strtotime($postData['endTime']) + 3600*24 -1;
                $query->where('i.addtime', '<=', $endTime);
            }
            if(isset($postData['beginTime']) && $postData['beginTime'] && isset($postData['endTime']) && $postData['endTime']){
                $endTime = strtotime($postData['endTime']) + 3600*24 -1;
                $query->where('i.addtime', [ '>=' , strtotime($postData['beginTime'])],['<=', $endTime], 'and');
            }
            if(isset($postData['mobile']) && $postData['mobile']){
                $query->where('u.mobile', $postData['mobile']);
            }
            if(isset($postData['userID']) && $postData['userID']){
                $query->where('i.userID', $postData['userID']);
            }

            $platformID = $this->userPlatform();
            if($platformID == 0){
                if(isset($postData['platformID'])   && $postData['platformID']){
                    $query->where('u.platformID', $postData['platformID']);
                }
            }else{
                $query->where('u.platformID', $platformID);
            }
        };
        $list = \app\model\UserMoneyLog::alias('i')
            ->join('user u', 'u.userID=i.userID', 'LEFT')
            ->field('i.*,u.mobile ,u.nickname')
            ->where($where)
            ->order('addtime desc')
            ->limit(( $page - 1 ) * $size , $size)
            ->select();
        if($list){
            $list->append(['turnoverText','ordertypeText']);
        }

        foreach($list as $key => $var){
            $var->data('nickname', emoji_decode($var['nickname']));
        }

        $count = \app\model\UserMoneyLog::alias('i')->join('user u', 'u.userID=i.userID', 'LEFT')->where($where)->count();
        $income = \app\model\UserMoneyLog::alias('i')->join('user u', 'u.userID=i.userID', 'LEFT')->where($where)->where('turnover', 1)->sum('amount');
        $expend = \app\model\UserMoneyLog::alias('i')->join('user u', 'u.userID=i.userID', 'LEFT')->where($where)->where('turnover', -1)->sum('amount');
        $expend =Tools::bcCalculate($expend, 100, '/');
        $income =Tools::bcCalculate($income, 100, '/');
        return $this->buildSuccess([
            'count'=>$count,
            'list'=>$list,
            'sumIncome'=>$income,
            'sumExpend'=>$expend
        ]);
    }

    public function awardList(){
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $postData = $this->request->get();
        $where = function($query)use($postData){
            if(isset($postData['type'])){
                $query->where('w.type', intval($postData['type']));
            }
            if(isset($postData['status'])){
                $query->where('w.status', intval($postData['status']));
            }
            if(isset($postData['mobile'])&& $postData['mobile']){
                $query->where('u.mobile', $postData['mobile']);
            }
            if(isset($postData['userID'])){
                $query->where('w.userID', intval($postData['userID']));
            }
            if(isset($postData['beginTime']) && $postData['beginTime']){
                $query->where('w.addTime', '>=', strtotime($postData['beginTime']));
            }
            if(isset($postData['endTime']) && $postData['endTime']){
                $endTime = strtotime($postData['endTime']) + 3600*24 -1;
                $query->where('w.addTime', '<=', $endTime);
            }
            if(isset($postData['beginTime']) && $postData['beginTime'] && isset($postData['endTime']) && $postData['endTime']){
                $endTime = strtotime($postData['endTime']) + 3600*24 -1;
                $query->where('w.addTime', [ '>=' , strtotime($postData['beginTime'])],['<=', $endTime], 'and');
            }

            $platformID = $this->userPlatform();
            if($platformID == 0){
                if(isset($postData['platformID'])   && $postData['platformID']){
                    $query->where('u.platformID', $postData['platformID']);
                }
            }else{
                $query->where('u.platformID', $platformID);
            }

        };
        $list = UserAward::alias('w')
            ->field('w.*,u.mobile,u.nickname,u.trueName')
            ->join('user u', 'u.userID=w.userID','LEFT')
            ->where($where)
            ->order('w.id desc')
            ->limit(( $page - 1 ) * $size , $size)
            ->select();
        if($list){
            $list->append(['statusText']);
        }

        foreach($list as $key => $var){
            $var->data('nickname', emoji_decode($var['nickname']));
            $var->data('trueName', $var['trueName'] ? : '');
        }

        $sumMoney = UserAward::alias('w')
            ->join('user u', 'u.userID=w.userID','LEFT')
            ->where($where)
            ->sum("w.money");

        $count = UserAward::alias('w')
            ->join('user u', 'u.userID=w.userID','LEFT')
            ->where($where)
            ->count();

        $sumMoney =Tools::bcCalculate($sumMoney, 100, '/');
        return $this->buildSuccess([
            'count'=>$count,
            'sumMoney'=>$sumMoney,
            'list' => $list
        ]);
    }

}

?>
